import { defineStore } from 'pinia';
import type { ShopBagType } from "../../views/home/index.vue"

import { GoodsItem } from "../../views/home/index.vue"
type State = {
    list: ShopBagType[]
}
export const useShopBagStore = defineStore('shopBag', {
    state: (): State => ({
        list: [],
    }),
    actions: {
        addShopBagList(item: GoodsItem) {
            const index = this.list.findIndex((good: ShopBagType) => good.id === item.id && good.token === localStorage.getItem('token'))
            index !== -1 ? this.list[index].count++ :
                this.list.push({ ...item, count: 1, token: localStorage.getItem('token') })
        },
        deleteShopBagList(item: GoodsItem) {
            const index = this.list.findIndex((good: ShopBagType) => good.id === item.id && good.token === localStorage.getItem('token'))
            this.list[index].count--
        }
    },
    persist: true, // 设置持久化
});
